<?php
ifi!$_GET['cmd']) {
	SQLvalidate($_GET['id']);
	SQLvalidate($_GET['rec']);

	if(!$_GET['irec'])
		$_GET['rec']=0;

	if(!perms_check('images', 'upload')) {
		redirect('index.php?module=error&error=auth_error');
		exit;
	}

	$fields=array('id', 'name');

	if($_GET['sort']!='ASC' && $_GET['sort'] != 'DESC')
		$_GET['sort']='ASC';
	if(!$_GET['sortby'])
		$_GET['sortby']='id';
	if($_GET['sort']=='ASC')
		$sortnext='DESC';
	else
		$sortnext='ASC';

	$content = string_template(read_file('admin/themes/watermark_header.php'), array("sortnext" => $sortnext));

	$db = new dbquery;

	$db->query("SELECT * FROM $conf[prefix]watermark") or $db->err(__FILE__, __LINE__);
	$q=$db->num_rows();

	$db->query("SELECT * FROM $conf[prefix]watermark ORDER by $_GET[sortby] $_GET[sort] LIMIT $_GET[rec], $conf[admin_per_page]") or $db->err(__FILE__, __LINE__); 

	while($d=$db->fetch_object()) {
		$i++;
		$edit='<a href="index.php?module=admin&action=watermark&cmd=edit&amp;id='.$d->id.'">'.$lang['admin_edit'].'</a>';
		$delete='<a href="index.php?module=admin&action=watermark&cmd=really_delete&amp;id='.$d->id.'">'.$lang['admin_delete'].'</a>';

		$content .= string_template(read_file('admin/themes/watermark_item.php'), array("id"=>$d->id, "name"=>$d->name, "image" => $conf['watermarks_dir'].'/'.$d->fname, "i"=>(($i%2)+1), "edit"=>$edit, "delete"=>$delete));
	}

	$page_link=split_to_pages('<a href="index.php?module=admin&action=watermark&sortby='.$_GET['sortby'].'&sort='.$_GET['sort'].'&amp;rec={rec}">{nr}</a>', $conf['admin_per_page'], $q, $_GET['rec']);
	$content .= string_template(read_file('admin/themes/watermark_footer.php'), array('page_link'=>$page_link));
}
elseif($_GET['cmd'] == 'add') {
	if(!perms_check('images', 'upload')) {
		redirect('index.php?module=error&error=auth_error');
		exit;
	}

	$content = string_template(read_file('admin/themes/watermark_form.php'), array("function" => 'watermark_add', "name"=>"", "id"=>"", 'checked' => ' CHECKED'));
}
elseif($_GET['cmd'] == 'edit') {
	SQLvalidate($_GET['id']);
	if(!perms_check('images', 'edit')) {
		redirect('index.php?module=error&error=auth_error');
		exit;
	}

	$db = new dbquery;
	$db->query("SELECT * FROM $conf[prefix]watermark WHERE id=$_GET[id]") or $db->err(__FILE__, __LINE__);

	if($db->num_rows()==0) {
		redirect("index.php?module=admin&amp;action=watermark");
		exit;
	}

	$d=$db->fetch_object();

	$content = string_template(read_file('admin/themes/watermark_form.php'), array("function" => 'watermark_edit', 'name'=>stripslashes($d->name), 'id'=>$d->id, 'filename' => $d->fname, 'src' => $conf['watermarks_dir'].$d->fname, 'checked' => ''));					 
}
elseif($_GET['cmd']=='really_delete') {
	SQLvalidate($_GET['id']);
	if(!perms_check('images', 'del')) {
		redirect('index.php?module=error&error=auth_error');
		exit;
	}

	$db = new dbquery;
	$db->query("SELECT * FROM $conf[prefix]watermark WHERE id=$_GET[id]") or $db->err(__FILE__, __LINE__);

	if($db->num_rows()==0) {
		redirect("index.php?module=admin&amp;action=watermark");
		exit;
	}

	$d = $db->fetch_object();

	$yes='<input type="button" onClick="javascript:location.href=\'index.php?module=watermark&function=watermark_del&amp;id='.$_GET['id'].'\'" value="'.$lang['yes'].'" class="button">';
	$no='<input type="button" onClick="javascript:history.go(-1);" value="'.$lang['no'].'" class="button">';

	$content=string_template($lang['admin_really_delete'], array('what'=>stripslashes($d->name), 'yes'=>$yes, 'no'=>$no));
}
$content=string_template(read_file('middle.php'), array('theme_path' => $GLOBALS['theme_path'], 'content' => $content, 'description' => $lang['watermark_watermark']));
?>
